package com.zhenmyan.secdeal.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description:
 *
 * rabbitmq都是现将消息发送给交换机，然后转发到队列中
 * 而转发有不同的方式，这里是fanout模式，即广播模式
 *
 * 配置类：需要设置队列和交换机名称，然后将队列与交换机进行绑定
 *
 * @author 颜真明
 * @date 2023/5/15  5:37
 */
@Configuration
public class RabbitMQFanoutConfig {

    // Fanout 交换机为广播模式
    private static final String FANOUT_QUEUE_1= "fanout_queue_1";
    private static final String FANOUT_QUEUE_2 = "fanout_queue_2";
    private static final String FANOUT_EXCHANGE = "fanout_exchange";

    @Bean
    public Queue fanoutQueue1(){
        return new Queue(FANOUT_QUEUE_1);
    }
    @Bean
    public Queue fanoutQueue2(){
        return new Queue(FANOUT_QUEUE_2);
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    /**
     * 将队列1绑定到fanout交换机
     *
     * @return
     */
    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    /**
     * 将队列2绑定到fanout交换机
     *
     * @return
     */
    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

}
